package TDALista;
import java.util.Iterator;

public class IteratorList<E> implements Iterator<E> {
	PositionList<E> list;
	Position<E> pos;
	
public IteratorList(PositionList<E> p){
		list=p;
		if (list.isEmpty())
			pos=null;
		else{
			
			 pos=null;}
			
		
	}
	
	/**
	 * Verifica si quedan elementos restantes en el iterador.
	 */
	public boolean hasNext(){
		boolean es=true;
		try{
			
		if (list.isEmpty() || pos==list.last())
			es=false;
		
		}
		catch (EmptyListException e){System.out.print(e.getMessage());}
	
	return es;
}
	
	/**
	 * Devuelve el elemento siguiente en el iterador.
	 */
	public E next(){
		E toreturn=null;
	
		try{
	   if (this.hasNext()){
		   if (pos==null){
			   toreturn=(E) list.first().element();
			   pos=list.first();
		   }
		   else
		   {pos=list.next(pos);
		   toreturn=pos.element();
		   }
		   
			
			}}
		
		  catch(InvalidPositionException e3){System.out.println("e.getMessage()");}
		  catch(BoundaryViolationException e2){System.out.println("e.getMessage()");}
		  catch(EmptyListException e1) {System.out.println("e.getMessage()");}

	  return toreturn;
	}
	

}